package com.longcai.service.impl.homegood;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.homegood.ClyjGoodsBrand;
import com.longcai.entity.homegood.ClyjGoodsClassBrand;
import com.longcai.entity.homegood.ClyjGoodsHomepage;
import com.longcai.entity.homepage.ClyjGoodsType;
import com.longcai.mapper.homegood.ClyjGoodsBrandMapper;
import com.longcai.mapper.homegood.ClyjGoodsClassBrandMapper;
import com.longcai.mapper.homegood.ClyjGoodsHomepageMapper;
import com.longcai.mapper.homepage.ClyjGoodsTypeMapper;
import com.longcai.service.homegood.ClyjGoodsBrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品品牌表 服务实现类
 * </p>
 *
 * @author LWK
 * @since 2019-03-22
 */
@Service
public class ClyjGoodsBrandServiceImpl extends ServiceImpl<ClyjGoodsBrandMapper, ClyjGoodsBrand> implements ClyjGoodsBrandService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjGoodsClassBrandMapper clyjGoodsClassBrandMapper;
    @Autowired
    private ClyjGoodsBrandMapper clyjGoodsBrandMapper;
    @Autowired
    private ClyjGoodsTypeMapper clyjGoodsTypeMapper;
    @Autowired
    private ClyjGoodsHomepageMapper clyjGoodsHomepageMapper;

    /**
     * 查询所有品牌
     *
     * @return
     */
    @Override
    public ApiResult getallbrand() {
        try {
            QueryWrapper<ClyjGoodsBrand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("brand_statu", "0");
            List<ClyjGoodsBrand> clyjGoodsBrands = this.clyjGoodsBrandMapper.selectList(queryWrapper);
            logger.debug("查询成功+++++++++", clyjGoodsBrands);
            return ApiResult.success(clyjGoodsBrands);
        } catch (Exception e) {
            logger.debug("查询失败+++++++++");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 品牌墙
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getbrandgoods(String id) {
        Map map = new HashMap();
        List<String> list = new ArrayList<String>();

        try {
            //查询分类下的品牌
            QueryWrapper<ClyjGoodsClassBrand> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("goods_class_id", id);
            List<ClyjGoodsClassBrand> clyjGoodsClassBrands = this.clyjGoodsClassBrandMapper.selectList(queryWrapper);
            //查询商品类型中间表
            QueryWrapper<ClyjGoodsType> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("type_id", id);
            List<ClyjGoodsType> clyjGoodsTypeList = this.clyjGoodsTypeMapper.selectList(queryWrapper1);
            //遍历取出对应商品id
            for (ClyjGoodsType clyjGoodsType : clyjGoodsTypeList) {
                list.add(clyjGoodsType.getGoodsId());
            }
            //遍历取出对应品牌id
            for (ClyjGoodsClassBrand clyjGoodsClassBrand : clyjGoodsClassBrands) {
                QueryWrapper<ClyjGoodsHomepage> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.in("goods_id", list);
                queryWrapper2.eq("goods_brand_id", clyjGoodsClassBrand.getGoodsBrandId());
                List<ClyjGoodsHomepage> clyjGoodsHomepageList = this.clyjGoodsHomepageMapper.selectList(queryWrapper2);
                clyjGoodsClassBrand.setClyjGoodsHomepageList(clyjGoodsHomepageList);
            }
            logger.debug("查询成功+++++++++", clyjGoodsClassBrands);
            return ApiResult.success(clyjGoodsClassBrands);
        } catch (Exception e) {
            logger.debug("查询失败+++++++++");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }
}
